﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using ParentChildLookup.Tools;
using ParentChildLookup.BO;

namespace ParentChildLookup.Controls
{
    public class ParentLookup : BaseLookupField
    {
        private DVDropDownList ddlLookup;
        private RequiredFieldValidator validator;

        protected override void CreateChildControls()
        {
            if (this.ControlMode == SPControlMode.Edit || this.ControlMode == SPControlMode.New)
            {
                ddlLookup = new DVDropDownList();
                ddlLookup.ID = "ParentLookupField";


                if (this.Field.Required)
                {
                    validator = new RequiredFieldValidator();
                    validator.ControlToValidate = ddlLookup.ID;
                    validator.Text = "*";
                }
                SPFieldLookup lookupField = (this.List.Fields.GetFieldByInternalName(this.FieldName) as SPFieldLookup);
                SPList sourceList;

                using (SPSite site = new SPSite(this.Web.Url))
                {
                    using (SPWeb web = site.OpenWeb(lookupField.LookupWebId))
                    {
                        sourceList = web.Lists[new Guid(lookupField.LookupList)];
                    }
                }

                ddlLookup.DataSource = sourceList.Items;
                ddlLookup.DataTextField = "Title";
                ddlLookup.DataValueField = "ID";
                ddlLookup.DataBind();
                ddlLookup.Items.Insert(0, new ListItem(string.Empty, string.Empty));
                ddlLookup.AutoPostBack = true;
                if (this.ListItemFieldValue != null)
                {
                    SPFieldLookupValue lookupValue = new SPFieldLookupValue(this.ListItemFieldValue.ToString());
                    ddlLookup.SelectedValue = lookupValue.LookupId.ToString();
                }

                this.Controls.Add(ddlLookup);
                if (this.Field.Required)
                    this.Controls.Add(validator);
            }
            else
                base.CreateChildControls();

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (this.ControlMode == SPControlMode.Edit || this.ControlMode == SPControlMode.New)
            {
                string key = parentChildLookup.ParentFieldInternalName;
                if (string.IsNullOrEmpty(ddlLookup.SelectedValue))
                {
                    if (this.ListItem.Properties.ContainsKey(key))
                        this.ListItem.Properties.Remove(key);
                }
                else
                {
                    if (this.ListItem.Properties.ContainsKey(key))
                        this.ListItem.Properties[key] = ddlLookup.SelectedValue;
                    else
                        this.ListItem.Properties.Add(key, ddlLookup.SelectedValue);
                }
            }
            this.ListItemFieldValue = ddlLookup.SelectedValue;
        }
    }
}
